package com.projet.whattowatch.adapter;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.activity.MovieDetailActivity;
import com.projet.whattowtach.utils.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
 
public class DiscoverFlipperAdapter extends ArrayAdapter<String>
{
	private final Activity context;
	private final ArrayList<String> name;
	private final ArrayList<String> image;
	private ImageLoader imageLoader;
	
	final String EXTRA_MOVIE_NAME = "movie_name";
	
	public DiscoverFlipperAdapter(Activity context, ArrayList<String> name, ArrayList<String> image)
	{
		super(context, R.layout.custom_viewflipper_home, name);
		this.context = context;
		this.name = name;
		this.image = image;
	}
	
	@Override
	public int getCount()
	{
        return name.size();
    }
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		LayoutInflater inflater = context.getLayoutInflater();
		View rootView = inflater.inflate(R.layout.custom_viewflipper_home, null);
		
		int loader = R.drawable.defaut_movie_img;
		Button imageView = (Button) rootView.findViewById(R.id.img_button);
		imageLoader = new ImageLoader(context);
		imageLoader.DisplayImage(image.get(position), loader, imageView);
		
		imageView.setOnClickListener(new OnClickListener()
		{
            @Override
            public void onClick(View v)
            {
            	String movieName = name.get(position);
		    	
		    	Intent movieDetail = new Intent(context, MovieDetailActivity.class);
		    	movieDetail.putExtra(EXTRA_MOVIE_NAME, movieName);
		    	context.startActivity(movieDetail);
            }
        });
		
		return rootView;
	}
}